/**
 * 字符串处理工具
 * @Author Ace.c
 * @Time 2016-08-09 10:22
 */
class StringUtils {

    /**
     * 获取字符串的字符长度
     * @returns {number}
     */
    static getCharLength(text: string): number {
        let len: number = 0;
        for (let i: number = 0; i < text.length; i++) {
            if (text.charCodeAt(i) >= 0 && text.charCodeAt(i) <= 128) {
                len += 1;
            }
            else {
                len += 2;
            }
        }
        return len;
    }

    /**
     * 获取00d00h00m00s
     * @returns {string}
     */
    static getTimeStr(sec: number): string {
        sec = sec < 0 ? 0 : sec;
        let d: number = Math.floor(sec / 86400);
        let h: number = Math.floor(sec % 86400 / 3600);
        let m: number = Math.floor(sec % 3600 / 60);
        let s: number = Math.floor(sec % 60);
        let str: string = "";
        if (d > 0) str += (d < 10 ? "0" + d : d) + "d";
        if (h > 0) str += (h < 10 ? "0" + h : h) + "h";
        if (m > 0) str += (m < 10 ? "0" + m : m) + "m";
        if (s > 0) str += (s < 10 ? "0" + s : s) + "s";
        return str;
    }

    /**
     * 获取00:00:00
     * @returns {string}
     */
    static getTimeStr2(sec: number): string {
        sec = sec < 0 ? 0 : sec;
        let h: number = Math.floor(sec / 3600);
        let m: number = Math.floor(sec % 3600 / 60);
        let s: number = Math.floor(sec % 60);
        return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
    }

    /**
     * 获取YYYY-MM-DD, 通过时间戳
     * @returns {string}
     */
    static getYTDByTimestamp(timestamp: number): string {
        timestamp = timestamp < 0 ? 0 : timestamp;

        let date: Date = new Date();
        date.setTime(timestamp);

        let Y: number = date.getFullYear();
        let M: number = date.getMonth() + 1;
        let D: number = date.getDate();
        return Y + "-" + (M < 10 ? "0" + M : M) + "-" + (D < 10 ? "0" + D : D);
    }

    /**
     * 获取MM月DD日, 通过时间戳
     * @returns {string}
     */
    static getYTFByTimestamp(timestamp: number): string {
        timestamp = timestamp < 0 ? 0 : timestamp;

        let date: Date = new Date();
        date.setTime(timestamp);

        let y: number = date.getFullYear();
        let Y: number = y % 100;
        let M: number = date.getMonth() + 1;
        let D: number = date.getDate();
        return (M < 10 ? "0" + M : M) + "月" + (D < 10 ? "0" + D : D) + "日";
    }

    /**
     * 获取00:00:00, 通过时间戳
     * @returns {string}
     */
    static getHMSByTimestamp(timestamp: number): string {
        timestamp = timestamp < 0 ? 0 : timestamp;

        let date: Date = new Date();
        date.setTime(timestamp);

        let h: number = date.getHours();
        let m: number = date.getMinutes();
        let s: number = date.getSeconds();
        return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
    }

    /**
     * 获取url参数
     * @param val
     * @returns {string}
     */
    static getUrlParams(val): string {
        let uri = window.location.search;
        let re = new RegExp("" + val + "=([^&?]*)", "ig");
        let pa = ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);
        console.log(val + ":" + pa);
        return pa;
    }

    /**
     * 获得1,000,000
     * @returns {string}
     */
    static getValueStr(value: number): string {
        let arr: string[] = value.toString().split("");
        let str: string = "";
        let len: number = arr.length;
        for (let i: number = 1; i <= len; i++) {
            str = arr.pop() + str;
            if (i % 3 == 0 && i != len) {
                str = "," + str;
            }
        }
        return str;
    }

    /**
     * 获得1.00k
     * @returns {string}
     */
    static getValueStr2(value: number): string {
        let str: string = "";
        let len: number = String(Math.floor(value)).length;

        if (len < 4) {
            str = String(Math.round(value));
        }
        else if (len < 7) {
            str = String(Math.floor(value / 100) / 10) + "K";
        }
        else if (len < 10) {
            str = String(Math.floor(value / 100000) / 10) + "M";
        }
        else if (len < 13) {
            str = String(Math.floor(value / 100000000) / 10) + "B";
        }
        else {
            str = String(Math.floor(value / 100000000000) / 10) + "T";
        }
        return str;
    }

    /**
     * 获取转换数字串
     */
    static getNumberStr(n: number): string {
        let arr: string[] = n.toString().split('');
        let str: string = '';

        if (arr.length <= 4) {
            str = arr.join('');
        }
        else {
            let remainder:number = arr.length % 4;

            if (remainder != 0) arr.splice(remainder, 0, '.');

            str = arr[0] + arr[1] + arr[2] + arr[3] + (remainder != 0 ? arr[4] : '');

            str += arr.length <= 8 ? '万' : '亿';
        }

        console.log('转换数字串', str);

        return str;
    }

    /**
     * 替换
     * @returns {string}
     */
    static replace(str: string, rep: string): string {
        let regExp: RegExp = new RegExp('\{[0-9]\}');
        return str.replace(regExp, rep);
    }

    /**
     * 删除空格
     * @returns {string}
     */
    static delSpace(str: string): string {
        let arr: string[] = str.split("");
        while (arr.indexOf(" ") != -1) {
            arr.splice(arr.indexOf(" "), 1);
        }
        return arr.join("");
    }
}